﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using ArmoryData;
using ArmoryData.Character;
using System.IO;
using System.ComponentModel;

namespace ArmoryViewer.Classes
{
    static class CharacterLoader
    {
        public delegate void LoaderProgressChangedEventHandler(object sender, LoaderProgressChangedEventArgs e);
        public static event LoaderProgressChangedEventHandler LoaderProgressChanged;
        public delegate void LoaderCompletedEventHandler(object sender, CharacterInfo c);
        public static event LoaderCompletedEventHandler LoaderCompleted;
        static BackgroundWorker bgwLoader;
        

        public static void LoadCharacter(string name, string realm)
        {
            bgwLoader = new BackgroundWorker();
            try
            {
                bgwLoader.WorkerReportsProgress = true;
                bgwLoader.DoWork += new DoWorkEventHandler(bgwLoader_DoWork);
                bgwLoader.ProgressChanged += new ProgressChangedEventHandler(bgwLoader_ProgressChanged);
                bgwLoader.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgwLoader_RunWorkerCompleted);
                OnLoaderProgressChanged(new LoaderProgressChangedEventArgs(0, "Downloading character information", null));
                bgwLoader.RunWorkerAsync(new string[] { name, realm });
            }
            catch (Exception ex)
            {
                OnLoaderProgressChanged(new LoaderProgressChangedEventArgs(100, "error", ex));
            }
        }

        static void bgwLoader_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Result is CharacterInfo)
            {
                OnLoaderCompleted(e.Result as CharacterInfo);
            }
        }

        static void bgwLoader_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.UserState is Exception)
                OnLoaderProgressChanged(new LoaderProgressChangedEventArgs(100, "error", e.UserState as Exception));
            else
                OnLoaderProgressChanged(new LoaderProgressChangedEventArgs(0, e.UserState as string, null));
        }

        static void bgwLoader_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] args = (string[])e.Argument;
            CharacterInfo c = new CharacterInfo(args[0], args[1], Settings.ArmoryUrl);

            try
            {
                c.DownloadCharacterInfo();
                bgwLoader.ReportProgress(1, "Downloading Items");
                c.DownloadItems(Settings.DownloadSockets, Settings.Language);
                e.Result = c;
            }
            catch (Exception ex)
            {
                e.Result = ex;
                bgwLoader.ReportProgress(100, ex);
            }
        }


        public static void Cancel()
        {
           // do nothing fo now
        }

        static void OnLoaderProgressChanged(LoaderProgressChangedEventArgs e)
        {
            if (LoaderProgressChanged == null) return;
            LoaderProgressChanged(bgwLoader, e);
        }

        static void OnLoaderCompleted(CharacterInfo character)
        {
            if (LoaderCompleted == null) return;
            LoaderCompleted(bgwLoader, character);
        }
    }
}
